package com.qo.qsdk.utils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventType;
import com.facebook.appevents.AppEventsConstants;
import com.facebook.appevents.AppEventsLogger;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.qo.qsdk.QSDK;
import com.qo.qsdk.param.GameRoleParams;

import java.util.HashMap;
import java.util.Map;

public class LogManager {

    private static final String TAG = LogManager.class.getSimpleName();

    private static class LogManagerHolder {
        private static final LogManager instance_ = new LogManager();
    }

    public static LogManager getInstance() {
        return LogManagerHolder.instance_;
    }

    public LogManager() {

    }

    public void init(Activity activity) {
        firstRun(activity);
    }

    private void firstRun(Activity activity) {
        if (isFirst(activity, "firstRun")) {
            try{
                //安装完成
                FirebaseManager.getInstance().log(activity, "own_install");
                //运行
                FirebaseManager.getInstance().log(activity, "launch");

                AppsFlyerManager.getInstance().log(activity, "own_install");
                AppsFlyerManager.getInstance().log(activity, "launch");

                FacebookManager.getInstance().log(activity, "own_install");
                FacebookManager.getInstance().log(activity, "launch");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void login(Activity activity) {
        try{
            String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

            Bundle bundle = new Bundle();
            bundle.putString("account", account);

            Map<String, Object> eventValue = new HashMap<String, Object>();
            eventValue.put("account", account);

            FirebaseManager.getInstance().log(activity, "login", bundle);
            AppsFlyerManager.getInstance().log(activity, "login", eventValue);
            FacebookManager.getInstance().log(activity, "login", bundle);
        }catch (Exception e){

        }
    }

    private boolean isFirst(Activity activity, String key) {
        if ((boolean) QSDKHelper.getInstance().getItem(activity, key, false)) {
            QSDKHelper.getInstance().setItem(activity, key, false);
            return true;
        }
        return false;
    }

    public void register(Activity activity) {
        try{
            if (isFirst(activity, "register")) {

                String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

                Bundle bundle = new Bundle();
                bundle.putString("account", account);
                FirebaseManager.getInstance().log(activity, "register", bundle);

                Map<String, Object> eventValue = new HashMap<String, Object>();
                eventValue.put(AFInAppEventParameterName.CONTENT_ID, account);
                eventValue.put("account", account);
                AppsFlyerManager.getInstance().log(activity,"register", eventValue);

                FacebookManager.getInstance().log(activity, "register", bundle);
            }
        }catch (Exception e){}
    }

    public void roleCreate(Activity activity, GameRoleParams p) {
        try{
            String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

            Bundle bundle = new Bundle();
            bundle.putString("account", account);

            Map<String, Object> eventValue = new HashMap<String, Object>();
            eventValue.put("account", account);

            FirebaseManager.getInstance().log(activity, "create_role", bundle);
            AppsFlyerManager.getInstance().log(activity, "create_role", eventValue);
            FacebookManager.getInstance().log(activity, "create_role", bundle);
        }catch (Exception e){

        }
    }

    public void roleLogin(Activity activity, GameRoleParams p) {
        try{
            String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

            Bundle bundle = new Bundle();
            bundle.putString("account", account);

            Map<String, Object> eventValue = new HashMap<String, Object>();
            eventValue.put("account", account);

            FirebaseManager.getInstance().log(activity, "login_role", bundle);
            AppsFlyerManager.getInstance().log(activity, "login_role", eventValue);
            FacebookManager.getInstance().log(activity, "login_role", bundle);
        }catch (Exception e){

        }
    }

    public void firstPurchase(Activity activity, GameRoleParams p) {
        if (isFirst(activity,"logFirstPurchase" + (p == null ? "0" : p.getRoleId()))) {
            try{
                FirebaseManager.getInstance().log(activity, "first_purchase");
                AppsFlyerManager.getInstance().log(activity, "first_purchase");
                FacebookManager.getInstance().log(activity, "first_purchase");
            }catch (Exception e){

            }
        }
    }

    public void maxPurchase(Activity activity, GameRoleParams p) {
        try{
            FirebaseManager.getInstance().log(activity, "purchase_rr");
            AppsFlyerManager.getInstance().log(activity, "purchase_rr");
            FacebookManager.getInstance().log(activity, "purchase_rr");
        }catch (Exception e){

        }
    }

    public void joinGangs(Activity activity, GameRoleParams p) {
        if (isFirst(activity,"logJoinGangs" + (p == null ? "0" : p.getRoleId()))) {
            try{
                FirebaseManager.getInstance().log(activity, "join_gangs");
                AppsFlyerManager.getInstance().log(activity, "join_gangs");
                FacebookManager.getInstance().log(activity, "join_gangs");
            }catch (Exception e){

            }
        }
    }

    public void finishGuide(Activity activity,GameRoleParams p) {
        if (isFirst(activity,"logFinishGuide" + (p == null ? "0" : p.getRoleId()))) {
            try{
                FirebaseManager.getInstance().log(activity, "finish_guide");
                AppsFlyerManager.getInstance().log(activity, "finish_guide");
                FacebookManager.getInstance().log(activity, "finish_guide");
            }catch (Exception e){

            }
        }
    }

    public void clickStore(Activity activity, GameRoleParams p) {
        try{
            FirebaseManager.getInstance().log(activity, "click_store");
            AppsFlyerManager.getInstance().log(activity, "click_store");
            FacebookManager.getInstance().log(activity, "click_store");
        }catch (Exception e){

        }
    }

    public void addFriends(Activity activity, GameRoleParams p) {
        try{
            FirebaseManager.getInstance().log(activity, "add_friends");
            AppsFlyerManager.getInstance().log(activity, "add_friends");
            FacebookManager.getInstance().log(activity, "add_friends");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void acceptFriends(Activity activity, GameRoleParams p) {
        try{
            FirebaseManager.getInstance().log(activity, "accept_friends");
            AppsFlyerManager.getInstance().log(activity, "accept_friends");
            FacebookManager.getInstance().log(activity, "accept_friends");
        }catch (Exception e){

        }
    }

    public void retention(Activity activity, int day, GameRoleParams p) {
        Log.e("aaa", "retention_" + day);
        if (isFirst(activity,"logRetention" + day + "_" + (p == null ? "0" : p.getRoleId()))) {
            switch (day) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 14:
                case 30:
                    try{
                        String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

                        Bundle bundle = new Bundle();
                        bundle.putString("account", account);

                        Map<String, Object> eventValue = new HashMap<String, Object>();
                        eventValue.put("account", account);

                        FirebaseManager.getInstance().log(activity, "retention_" + day, bundle);
                        AppsFlyerManager.getInstance().log(activity, "retention_" + day, eventValue);
                        FacebookManager.getInstance().log(activity, "retention_" + day, bundle);
                    }catch (Exception e){

                    }
                    break;
            }
        }
    }

    public void level(Activity activity, GameRoleParams p) {
        try{
            int level = Integer.parseInt(p.getRoleLevel());
            if (level == 25 || level == 75 || level == 100 || level == 120 || level == 150 || level == 200 || level == 250 || level == 300) {
                String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

                Bundle bundle = new Bundle();
                bundle.putString("account", account);

                Map<String, Object> eventValue = new HashMap<String, Object>();
                eventValue.put("account", account);

                FirebaseManager.getInstance().log(activity, "level_" + p.getRoleLevel(), bundle);
                AppsFlyerManager.getInstance().log(activity, "level_" + p.getRoleLevel(), eventValue);
                FacebookManager.getInstance().log(activity, "level_" + p.getRoleLevel(), bundle);
            }

        }catch (Exception e){

        }
    }

    public void gameTime(Activity activity, GameRoleParams p) {
        try{
            long time = p.getOnlineTime() / 60;
            if (time == 1 || time == 10 || time ==30 || time == 60 || time == 120 || time == 180) {
                String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();

                Bundle bundle = new Bundle();
                bundle.putString("account", account);

                Map<String, Object> eventValue = new HashMap<String, Object>();
                eventValue.put("account", account);

                FirebaseManager.getInstance().log(activity, "game_time_" + p.getOnlineTime(), bundle);
                AppsFlyerManager.getInstance().log(activity, "game_time_" + p.getOnlineTime(), eventValue);
                FacebookManager.getInstance().log(activity, "game_time_" + p.getOnlineTime(), bundle);
            }
        }catch (Exception e){

        }
    }

    public void purchase(Activity activity, String currencyCode, double amount, String productId) {

        String account = "";
        try {
            account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();
        }catch (Exception e){}

        try{
            Bundle bundle = new Bundle();
            bundle.putString(FirebaseAnalytics.Param.ITEM_ID, productId);
            bundle.putString(FirebaseAnalytics.Param.CURRENCY, currencyCode);
            bundle.putDouble(FirebaseAnalytics.Param.VALUE, amount);
            bundle.putString("account", account);
            FirebaseManager.getInstance().log(activity, "purchase", bundle);

        }catch (Exception e){}

        try {
            Bundle bundleFb = new Bundle();
            bundleFb.putString(AppEventsConstants.EVENT_PARAM_CONTENT_ID, productId);
            bundleFb.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, currencyCode);
            bundleFb.putDouble(AppEventsConstants.EVENT_PARAM_VALUE_TO_SUM, amount);
            bundleFb.putString("account", account);
            FacebookManager.getInstance().log(activity, AppEventsConstants.EVENT_NAME_PURCHASED, bundleFb);
        }catch (Exception e){}

        try {
            Map<String, Object> eventValue = new HashMap<String, Object>();
            eventValue.put(AFInAppEventParameterName.REVENUE,amount);
            eventValue.put(AFInAppEventParameterName.CONTENT_ID,productId);
            eventValue.put(AFInAppEventParameterName.CURRENCY,currencyCode);
            eventValue.put("account", account);
            AppsFlyerManager.getInstance().log(activity, AFInAppEventType.PURCHASE, eventValue);
        }catch (Exception e){}
    }

    public void purchaseSev(Activity activity, String currencyCode, double amount, String productId){
        try{
            String account = QSDKHelper.getInstance().getUserInfoModel(activity).getAccount();
            Bundle bundle = new Bundle();
            bundle.putString(FirebaseAnalytics.Param.ITEM_ID, productId);
            bundle.putString(FirebaseAnalytics.Param.CURRENCY, currencyCode);
            bundle.putDouble(FirebaseAnalytics.Param.VALUE, amount);
            bundle.putString("account", account);
            FirebaseManager.getInstance().log(activity, "purchase_sev_1", bundle);

        }catch (Exception e){}
    }
}
